package com.smart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.common.PageInfo;
import com.smart.model.dtomapper.OperatePermissionDtoMapper;
import com.smart.model.domain.OperatePermissionDO;
import com.smart.mapper.OperatePermissionMapper;
import com.smart.model.request.OperatePermissionRequest;
import com.smart.model.vo.OperateTreePermissionVO;
import com.smart.service.OperatePermissionService;
import com.smart.model.convert.OperatePermissionConvert;
import com.smart.model.vo.OperatePermissionVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhanghong
 */
@Service
public class OperatePermissionServiceImpl extends ServiceImpl<OperatePermissionMapper, OperatePermissionDO> implements OperatePermissionService {
    @Autowired
    private OperatePermissionMapper operatePermissionMapper;

    @Override
    public OperatePermissionVO getByKey(long id) {
        OperatePermissionDO operatePermissionDO = getById(id);
        OperatePermissionVO operatePermissionVO = OperatePermissionDtoMapper.INSTANCE.do2VO(operatePermissionDO);
        return operatePermissionVO;
    }

    @Override
    public PageInfo<OperatePermissionVO> pageQuery(OperatePermissionRequest request) {
        long count = operatePermissionMapper.pageCount(request);
        List<OperatePermissionDO> list = operatePermissionMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<OperatePermissionVO> operatePermissionVOList = OperatePermissionConvert.buildOperatePermissionVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), operatePermissionVOList, count);
    }

    @Override
    public List<OperatePermissionVO> listQuery(OperatePermissionRequest request) {
        List<OperatePermissionDO> list = list();
        List<OperatePermissionVO> operatePermissionVOS = OperatePermissionConvert.buildOperatePermissionVoList(list);
        return operatePermissionVOS;
    }

    @Override
    public Boolean add(OperatePermissionRequest request) {
        OperatePermissionDO operatePermissionDO = OperatePermissionDtoMapper.INSTANCE.request2DO(request);
        save(operatePermissionDO);
        return true;
    }

    @Override
    public Boolean updateByKey(OperatePermissionRequest request) {
        OperatePermissionDO operatePermissionDO = OperatePermissionDtoMapper.INSTANCE.request2DO(request);
        save(operatePermissionDO);
        return true;
    }

    @Override
    public List<OperateTreePermissionVO> listAllPermission() {
        List<OperateTreePermissionVO> rets = Lists.newArrayList();
        List<OperatePermissionDO> permissions = operatePermissionMapper.findByParentId(0L);
        for(OperatePermissionDO item : CollectionUtils.emptyIfNull(permissions)){
            OperateTreePermissionVO treeVO = OperatePermissionDtoMapper.INSTANCE.do2TreeVO(item);
            List<OperatePermissionDO> childs = operatePermissionMapper.findByParentId(item.getId());
            if (CollectionUtils.isNotEmpty(childs)) {
                List<OperateTreePermissionVO> childVos = childs.stream().map(OperatePermissionDtoMapper.INSTANCE::do2TreeVO).collect(Collectors.toList());
                for (OperateTreePermissionVO childVo : childVos) {
                    childVo.setChildren(deepSearch(childVo));
                }
                treeVO.setChildren(childVos);
            }
            rets.add(treeVO);
        }
        return rets;
    }

    /**
     * 递归查询权限子菜单
     * @param permissionVO
     * @return
     */
    private List<OperateTreePermissionVO> deepSearch(OperateTreePermissionVO permissionVO) {
        if (permissionVO == null) {
            return Collections.emptyList();
        }
        List<OperatePermissionDO> childs = operatePermissionMapper.findByParentId(permissionVO.getId());
        if (CollectionUtils.isNotEmpty(childs)) {
            List<OperateTreePermissionVO> childVos = childs.stream().map(OperatePermissionDtoMapper.INSTANCE::do2TreeVO).collect(Collectors.toList());
            for (OperateTreePermissionVO childVo : childVos) {
                childVo.setChildren(deepSearch(childVo));
            }
            permissionVO.setChildren(childVos);
            return childVos;
        }
        return Collections.emptyList();
    }
}
